package com.newppjoke.ktnewppjoke.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MenuItem;

import com.google.android.material.bottomnavigation.BottomNavigationItemView;
import com.google.android.material.bottomnavigation.BottomNavigationMenuView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.bottomnavigation.LabelVisibilityMode;
import com.newppjoke.ktnewppjoke.R;
import com.newppjoke.ktnewppjoke.bean.BottomBar;
import com.newppjoke.ktnewppjoke.bean.Destination;
import com.newppjoke.ktnewppjoke.utils.AppConfig;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * Project Name: ktnewppjoke
 * File Name:    AppBottomBar.java
 * ClassName:    AppBottomBar
 *
 * Description: TODO.
 *
 * @author My start
 * @date 2022年10月11日 3:36 下午AppBottonmBar
 *
 * Copyright (c) 2022年,  new Network CO.ltd. All Rights Reserved.
 */
public class AppBottomBar extends BottomNavigationView
{
    private static int[] sIcons = new int[]{R.mipmap.icon_tab_home, R.mipmap.icon_tab_sofa, R.mipmap.icon_tab_publish, R.mipmap.icon_tab_find, R.mipmap.icon_tab_mine};
    private BottomBar bootomBar;

    public AppBottomBar(Context context) {
        this(context, null);
    }

    public AppBottomBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @SuppressLint("RestrictedApi")
    public AppBottomBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);

        bootomBar = AppConfig.getBottomBarConfig();
        List<BottomBar.Tab> tabs = bootomBar.tabs;
        int[][] states = new int[2][];
        states[0] = new int[]{android.R.attr.state_selected};
        states[1] =new int[]{};

        int[] colors = new int[]{Color.parseColor(bootomBar.activeColor), Color.parseColor(
                bootomBar.inActiveColor)};

        ColorStateList stateList = new ColorStateList(states,colors);
        setItemTextColor(stateList);
        setItemIconTintList(stateList);
        //LABEL_VISIBILITY_LABELED:设置按钮的文本为一直显示模式
        //LABEL_VISIBILITY_AUTO:当按钮个数小于三个时一直显示，或者当按钮个数大于3个且小于5个时，被选中的那个按钮文本才会显示
        //LABEL_VISIBILITY_SELECTED：只有被选中的那个按钮的文本才会显示
        //LABEL_VISIBILITY_UNLABELED:所有的按钮文本都不显示
        setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED);
        setSelectedItemId(bootomBar.selectTab);

        for (int i = 0; i < tabs.size(); i++)
        {
            BottomBar.Tab tab = tabs.get(i);
            if (!tab.enable){
                continue;
            }
            int itemId = getItemId(tab.pageUrl);
            if (itemId<0){
                return;
            }

            MenuItem menuItem = getMenu().add(0, itemId, tab.index, tab.title);
            menuItem.setIcon(sIcons[tab.index]);
        }

        for (int i = 0; i <tabs.size(); i++)
        {
            BottomBar.Tab tab = tabs.get(i);
            int dp2px = dp2px(tab.size);
            BottomNavigationMenuView menuView = (BottomNavigationMenuView) getChildAt(0);
            BottomNavigationItemView itemView = (BottomNavigationItemView) menuView.getChildAt(tab.index);
            itemView.setIconSize(dp2px);
            if (TextUtils.isEmpty(tab.title)){
                itemView.setIconTintList(ColorStateList.valueOf(Color.parseColor(tab.tintColor)));
                itemView.setShifting(false);
            }
        }

    }

    private int dp2px(int size)
    {

        float val = getContext().getResources()
                              .getDisplayMetrics().density * size + 0.5f;
        return (int) val;
    }

    private int getItemId(String pageUrl) {
        Destination destination = AppConfig.getDestConfig().get(pageUrl);
        if (destination == null)
            return -1;
        return destination.id;
    }
}
